/**
*      author: Y_Yao
*      created: 2025-01-24 20:35:23
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 8e5 + 7;
int S[N],F[N],dp[N];
int n;

void solve()
{
    cin >> n;
    for(int i = 1;i <= n;i++)
    {
        cin >> S[i] >> F[i];
    }

    memset(dp,-INF,sizeof dp);
    int ans = 0;
    dp[400000] = 0;
    for(int i = 1;i <= n;i++)
    {
        if(S[i] >= 0)
        {
            for(int j = 800000;j >= S[i];j--)
            {
                dp[j] = max(dp[j],dp[j-S[i]] + F[i]);
            }
        }
        else
        {
            for(int j = 0;j <= 800000 + S[i];j++)
            {
                dp[j] = max(dp[j],dp[j-S[i]] + F[i]);
            }
        }
    }

    for(int i = 4e5;i <= 8e5;i++)
    {
        if(dp[i] > 0)
        {
            ans = max(ans,i + dp[i] - 400000);
        }
    }

    cout << ans << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}